import { getSourceList, uploadImage, createFolder, editFolder, delSource, delFolder, getImageDir, getSourceInfo, getSourceMoreInfo, getWxSourceInfo2,
         moveSourceDir, thumbSource } from '@/service/getData'
import * as type from '@/store/mutation-types'
const state = {
  sourceList: { // 素材列表
    list: [],
    page: {}
  },
  imgList: { // 图片列表
    list: [],
    page: {}
  },
  audioList: { // 音频列表
    list: [],
    page: {}
  },
  videoList: { // 视频列表
    list: [],
    page: {}
  },
  sourceDir: [], // 素材库目录
  currentDir: null, // 当前目录ID
  selected: {}, // 选中的素材
  sourceInfo: { // 素材信息
    type: '',
    mediaid: ''
  }
}
const actions = {
  // 素材库
  async getSourceList ({commit, state}, payload) {
    let res = await getSourceList(payload)
    commit(type.GET_SOURCE_LIST, {...res.data.data, sourceType: payload.type})
  },
  async uploadImage ({commit, state}, payload) {
    let res = await uploadImage(payload)
    return res
  },
  async delSource ({commit, state}, payload) {
    let res = await delSource(payload)
    return res
  },
  async delFolder ({commit, state}, id) {
    let res = await delFolder(id)
    return res
  },
  async createFolder ({commit, state}, payload) {
    let res = await createFolder(payload)
    return res
  },
  async editFolder ({commit, state}, payload) {
    let res = await editFolder(payload)
    return res
  },
  async getImageDir ({commit, state}) {
    let res = await getImageDir()
    console.log(res)
    if (res.data.data) {
      commit(type.GET_IMAGE_DIR, res.data.data)
    }
    return res
  },
  async getSourceInfo ({commit, state}, payload) {
    let res = await getSourceInfo(payload)
    if (res.data.data && res.data.data.mediaid) {
      let res2 = await getSourceMoreInfo(res.data.data.mediaid)
      commit(type.SET_SOURCE_INFO, Object.assign(res.data.data, res2.data.data))
      return res2
    } else {
      commit(type.SET_SOURCE_INFO, res.data.data)
      return res
    }
  },
  async getSourceMoreInfo ({commit, state}, mediaid) {
    let res = await getSourceMoreInfo(mediaid)
    commit(type.SET_SOURCE_INFO, res.data.data)
    return res
  },
  async getWxSourceInfo2 ({commit, state}, sourceId) {
    let res = await getWxSourceInfo2(sourceId)
    commit(type.SET_SOURCE_INFO, res.data.data)
  },
  async moveSourceDir ({commit, state}, payload) {
    let res = await moveSourceDir(payload)
    return res
  },
  async thumbSource ({commit, state}, sourceId) {
    let res = await thumbSource(sourceId)
    return res
  }
}
const mutations = {
  // 素材库
  [type.GET_SOURCE_LIST] (state, payload) {
    state.sourceList = payload
    const sourceType = payload.sourceType
    if (sourceType === 1) {
      state.videoList = payload
    } else if (sourceType === 2) {
      state.imgList = payload
    } else if (sourceType === 3) {
      state.audioList = payload
    }
  },
  [type.SET_CURRENT_DIR] (state, payload) {
    state.currentDir = payload
  },
  [type.GET_IMAGE_DIR] (state, payload) {
    state.sourceDir = payload
  },
  [type.SELECT_SOURCE] (state, payload) {
    state.selected = payload
  },
  [type.SET_SOURCE_INFO] (state, payload) {
    state.sourceInfo = payload
  },
  [type.UPDATE_SOURCE_INFO] (state, payload) {
    state.sourceInfo = Object.assign(state.sourceInfo, payload)
  },
  [type.INIT_SOURCE_INFO] (state, payload) {
    state.sourceInfo = {
      type: '',
      mediaid: ''
    }
  }
}
export default {
  state,
  actions,
  mutations
}
